package com.zzyl.serve.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.RedisConstants;
import com.zzyl.serve.domain.DeviceData;
import com.zzyl.serve.domain.Room;
import com.zzyl.serve.mapper.RoomMapper;
import com.zzyl.serve.service.IRoomService;
import com.zzyl.serve.vo.DeviceInfo;
import com.zzyl.serve.vo.RoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author howstrong
 * @date 2025-03-03
 */
@Service
@Slf4j
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 根据房间id查询房间数据(楼层、房间、价格)
     *
     * @param id
     */
    @Override
    public RoomVo getOneById(Long id) {
        return roomMapper.getOneById(id);
    }


    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取房间中的智能设备及数据
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {

        // 1.从数据库查询房间的基础信息(包含房间数据, 老人数据, 房间关联设备, 床位关联的设备)
        List<RoomVo> roomVoList = roomMapper.getRoomsWithDeviceByFloorId(floorId);

        if (ObjUtil.isEmpty(roomVoList)) {
            log.info("房间数据为空");
            return null;
        }

        // 2.遍历房间列表 从redis查询房间相关设备数据, 床位相关设备数据
        roomVoList.forEach(roomVo -> {
            // 2.1 遍历房间关联的设备列表
            roomVo.getDeviceVos().forEach(deviceInfo -> {
                // 2.1.1 从redis查询房间相关设备数据
                String jsonStr = (String) redisTemplate.opsForHash().get(RedisConstants.IOT_DEVICE_LAST_DATA, deviceInfo.getIotId());
                if (StrUtil.isEmpty(jsonStr)) {
                    log.info("设备数据为空");
                    return;
                }
                // 2.1.2 将json字符串转换为对象
                List<DeviceData> deviceDataList = JSONUtil.toList(jsonStr, DeviceData.class);
                // 4. 封装RoomVo 返回数据
                deviceInfo.setDeviceDataVos(deviceDataList);

            });
            // 3. 从redis查询床位相关设备数据
            roomVo.getBedVoList().forEach(bedVo -> {
                // 3.1 遍历床位关联的设备列表
                bedVo.getDeviceVos().forEach(deviceInfo -> {
                    // 3.2 从redis查询床位相关设备数据
                    String jsonStr = (String) redisTemplate.opsForHash().get(RedisConstants.IOT_DEVICE_LAST_DATA, deviceInfo.getIotId());
                    if (StrUtil.isEmpty(jsonStr)) {
                        log.info("设备数据为空");
                        return;
                    }
                    // 3.3 将json字符串转换为对象
                    List<DeviceData> deviceDataList = JSONUtil.toList(jsonStr, DeviceData.class);
                    // 4. 封装RoomVo返回数据
                    deviceInfo.setDeviceDataVos(deviceDataList);

                });
            });

        });

        return roomVoList;
    }
}
